'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

''' <summary>
''' Represents a Property.
''' </summary>
Public Class PropertyDescriptor
    Private _IsString As Boolean
    Private _IsValueType As Boolean
    Private _IsIListDescendant As Boolean
    Private _Info As System.Reflection.PropertyInfo

    Public Sub New(ByVal info As System.Reflection.PropertyInfo)
        _Info = info
        _IsValueType = info.PropertyType.IsValueType
        _IsString = info.PropertyType Is GetType(String)
        _IsIListDescendant = PropertyType.GetInterface(GetType(IList).ToString) IsNot Nothing
    End Sub

    ''' <summary>
    ''' Gets a value indicating whether the property type is a value type.
    ''' </summary>
    Public ReadOnly Property IsValueType() As Boolean
        Get
            Return _IsValueType
        End Get
    End Property

    ''' <summary>
    ''' Gets a value indicating whether the property type is System.String.
    ''' </summary>
    Public ReadOnly Property IsString() As Boolean
        Get
            Return _IsString
        End Get
    End Property

    ''' <summary>
    ''' Gets a value indicating whether the property type is a reference type.
    ''' </summary>
    ''' <remarks>IsReferenceType is false for System.String.</remarks>
    Public ReadOnly Property IsReferenceType() As Boolean
        Get
            Return Not _IsValueType And Not _IsString
        End Get
    End Property

    ''' <summary>
    ''' Gets a value indicating whether the property is readonly.
    ''' </summary>
    Public ReadOnly Property IsReadOnly() As Boolean
        Get
            Return Not _Info.CanWrite
        End Get
    End Property

    ''' <summary>
    ''' Gets a value indicating whether the property type is a descendant of System.Collections.IList.
    ''' </summary>
    Public ReadOnly Property IsIListDescendant() As Boolean
        Get
            Return _IsIListDescendant
        End Get
    End Property

    ''' <summary>
    ''' Gets the property name.
    ''' </summary>
    Public ReadOnly Property PropertyName() As String
        Get
            Return _Info.Name
        End Get
    End Property

    ''' <summary>
    ''' Gets the property type.
    ''' </summary>
    Public ReadOnly Property PropertyType() As Type
        Get
            Return _Info.PropertyType
        End Get
    End Property

    ''' <summary>
    ''' Sets the value of the property.
    ''' </summary>
    Public Sub SetValue(ByVal obj As Object, ByVal value As Object)
        _Info.SetValue(obj, value, Nothing)
    End Sub

    ''' <summary>
    ''' Returns the value of the property.
    ''' </summary>
    Public Function GetValue(ByVal obj As Object) As Object
        Return _Info.GetValue(obj, Nothing)
    End Function
End Class